package com.hs.srm.task;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import com.hs.srm.admin.modules.orders.entity.StockEntity;
import com.hs.srm.admin.modules.orders.service.StockService;
import com.hs.srm.admin.modules.requisition.entity.NproPurchRequestItemEntity;
import com.hs.srm.admin.modules.requisition.service.NproPurchRequestItemService;
import com.hs.srm.admin.modules.ssess.entity.SsessEntity;
import com.hs.srm.admin.modules.ssess.service.SsessService;
import com.hs.srm.common.utils.DateUtils;
import sunnykid.lang.SunnykidDate;
import sunnykid.task.SunnykidTask;

public class MyTask extends SunnykidTask {

	private NproPurchRequestItemService nproPurchRequestItemService;
	private  SsessService ssessService;
	private  StockService stockService;
	
	public MyTask(NproPurchRequestItemService nproPurchRequestItemService,SsessService ssessService,StockService stockService) {
		this.nproPurchRequestItemService = nproPurchRequestItemService;
		this.ssessService = ssessService;
		this.stockService = stockService;
	}

	@Override
	public void excute() {
		//这里的定时任务的代码执行块
		System.out.println("我是定时任务，我运行了！" + new SunnykidDate().getStandardDateTime());
		//**************************************************/
		double JQ_GRADE = 0; //交期最终得分；
		double JH_NUM_GRADE = 0; //交货数量
		double JG_GRADE = 0; //价格分数
		List<NproPurchRequestItemEntity> list = nproPurchRequestItemService.selectRequestItem();
		String purchRequestCodetem =""; //采购申请临时编码
		int applyQty=0;        //订单数量
		 // 不同类型转为BigDecimal
		BigDecimal maapplyQty = new BigDecimal(applyQty);
		SsessEntity ssessEntity = new SsessEntity();
		double flat = 0;
		for(NproPurchRequestItemEntity nproPurchRequestItemEntity :list){
			Date date = nproPurchRequestItemEntity.getDeliveryDate(); //得到交货日期
			String purchRequestCode =nproPurchRequestItemEntity.getPurchRequestCode(); //采购申请编码
			//获取订单号
			String poNo = nproPurchRequestItemEntity.getPoNo();
			int shippedQty = 0;
			BigDecimal mashippedQty = new BigDecimal(shippedQty);
			List<StockEntity> listStock = stockService.selectStockByDdh(poNo); //获取期内实际交货量
			if(listStock.size()>0){
				for(StockEntity stock : listStock){
					mashippedQty = mashippedQty.add(stock.getShippedQty());
				}
				if(!purchRequestCodetem.equals(purchRequestCode)){
					purchRequestCodetem = purchRequestCode;
				}
				//防止出现空指针问题
				if(null!=purchRequestCodetem&&purchRequestCodetem.equals(purchRequestCode)){
					maapplyQty = maapplyQty.add(nproPurchRequestItemEntity.getApplyQty());//订单申请总数量
					System.out.println("----------->??"+maapplyQty);
				}
			}
			
			java.util.Date deliveryDate = DateUtils.stringToDate(DateUtils.format(date, DateUtils.DATE_PATTERN), DateUtils.DATE_PATTERN);	//格式货交货日期
			java.util.Date newDate = DateUtils.stringToDate(DateUtils.format(DateUtils.currentDateTime(), DateUtils.DATE_PATTERN), DateUtils.DATE_PATTERN); //系统时间格式话
			int days =  DateUtils.differentDaysByMillisecond(newDate, deliveryDate); //交期减去系统日期
			//得到交货日期和系统时间相差几天
			//总分20分  交期占用10分
			if(days>=1&&days<=7){
				//扣除3分
				JQ_GRADE = SsessEntity.JQ_GRADE -3;
			}
			if(days>7&&days<=14){
				//扣除5分
				JQ_GRADE = SsessEntity.JQ_GRADE -5;
			}
			if(days>15){
				//扣除10分
				JQ_GRADE = SsessEntity.JQ_GRADE -10;
			}
			if(days<=0){
				//不扣分
				JQ_GRADE = SsessEntity.JQ_GRADE -0;
				JH_NUM_GRADE = SsessEntity.JH_NUM_GRADE;
			}
			if(days>0){
				//交货量占10分
				if(maapplyQty.intValue()!=0){
					//mashippedQty 收货数量
					//maapplyQty 订单申请数量
					BigDecimal div = mashippedQty.divide(maapplyQty,10,BigDecimal.ROUND_HALF_DOWN);//交货比例
					//四舍五入法
					div = div.setScale(4, BigDecimal.ROUND_HALF_UP);
					DecimalFormat df = new DecimalFormat("0.00%");
					//System.out.println("订单数量："+mashippedQty+"收货数量"+mashippedQty);
			        //按照交货比例算出对应的分数
			        JH_NUM_GRADE = SsessEntity.JH_NUM_GRADE * Double.parseDouble(df.format(div).substring(0, df.format(div).length()-1))/100;
			        //价格的合理性
			        BigDecimal taxPrice = nproPurchRequestItemEntity.getTaxPrice(); //供应商供货的价格
			        //当前价格减去最低价格
			        BigDecimal bignum = (taxPrice==null)?new BigDecimal(0):taxPrice.subtract(new BigDecimal(5));  //最低价格(后期要从信息记录维护进去的)
			        //差价比率
			        BigDecimal bignum1 =  (taxPrice==null)?new BigDecimal(0):taxPrice.subtract(bignum).divide(new BigDecimal(5),10,BigDecimal.ROUND_HALF_DOWN);
			        //截取
			        flat = Double.parseDouble(df.format(bignum1).substring(0, df.format(div).length()-2));
				}
			}
	        if(flat>0&&flat<=2){
	        	JG_GRADE = SsessEntity.JG_GRADE -1;
	        }
	        if(flat>=3&&flat<=4){
	        	JG_GRADE = SsessEntity.JG_GRADE -2;
	        }
	        if(flat>=5&&flat<=6){
	        	JG_GRADE = SsessEntity.JG_GRADE -3;
	        }
	        if(flat>=7&&flat<=8){
	        	JG_GRADE = SsessEntity.JG_GRADE -4;
	        }
	        if(flat>=9&&flat<=10){
	        	JG_GRADE = SsessEntity.JG_GRADE -5;
	        }
	        if(flat>=11&&flat<=12){
	        	JG_GRADE = SsessEntity.JG_GRADE -6;
	        }
	        if(flat>=13&&flat<=14){
	        	JG_GRADE = SsessEntity.JG_GRADE -7;
	        }
	        if(flat>=15&&flat<=16){
	        	JG_GRADE = SsessEntity.JG_GRADE -8;
	        }
	        if(flat>=17&&flat<=18){
	        	JG_GRADE = SsessEntity.JG_GRADE -9;
	        }
	        if(flat>=19){
	        	JG_GRADE = SsessEntity.JG_GRADE -10;
	        }
            if(flat<=0){
            	JG_GRADE =SsessEntity.JG_GRADE;
            }
            //设置关于交货的的评分
            Date dat = new Date();
            
          //  ssessEntity.setMarkId(SunnykidText.getUUID());
            ssessEntity.setMarkId(poNo);//订单号做为id
	        ssessEntity.setDeliveryTimeMark(JQ_GRADE+JH_NUM_GRADE);
	        ssessEntity.setPriceMark(JG_GRADE);
	        ssessEntity.setCreaDate(dat);
	        //供应商编码
	        ssessEntity.setVendorCode(nproPurchRequestItemEntity.getVendorCode());
	        //供应商名称
	        ssessEntity.setVendorName(nproPurchRequestItemEntity.getVendorName());
	        this.ssessService.insertOrUpdate(ssessEntity);
	       
	        //每循环一次分数重置0
	        JQ_GRADE = 0;
	        JH_NUM_GRADE = 0;
	        JG_GRADE = 0;
		}
		//*************************************************/
	}

	@Override
	public boolean resume() {
		return false;
	}

}
